import { Rule } from 'antd/es/form';
import { TruckPreOrder, TruckOrderAddress } from '../../types';
import type { IntlShape } from 'react-intl';

interface AddressValidationResult {
  isValid: boolean;
  message: string;
}

export function validateOrderAddress(formData: TruckPreOrder, errors: string[], intl: IntlShape): boolean {
  const shipperAddress = formData.shipperAddress;
  const deliveryAddress = formData.deliveryAddress;

  if (!shipperAddress?.oaCompany) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.company.required' }));
  }
  if (!shipperAddress?.oaEmail) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.email.required' }));
  } else if (!isValidEmail(shipperAddress?.oaEmail)) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.email.format' }));
  }
  if (!shipperAddress?.oaTelphone) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.phone.required' }));
  }
  if (!shipperAddress?.openTime) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.openTime.required' }));
  }
  if (!shipperAddress?.closeTime) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.closeTime.required' }));
  }
  if (!shipperAddress?.pickReadyDatetime) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.pickReady.required' }));
  }
  if (!shipperAddress?.accessorialsArray || shipperAddress?.accessorialsArray.length === 0) {
    errors.push(intl.formatMessage({ id: 'validation.shipper.accessorials.required' }));
  } else {
    if (shipperAddress?.accessorials) {
      shipperAddress.accessorials = cleanNoneData(shipperAddress?.accessorials);
    }
  }

  if (!deliveryAddress?.oaCompany) {
    errors.push(intl.formatMessage({ id: 'validation.receiver.company.required' }));
  }
  if (!deliveryAddress?.oaEmail) {
    errors.push(intl.formatMessage({ id: 'validation.receiver.email.required' }));
  } else if (!isValidEmail(deliveryAddress?.oaEmail)) {
    errors.push(intl.formatMessage({ id: 'validation.receiver.email.format' }));
  }
  if (!deliveryAddress?.oaTelphone) {
    errors.push(intl.formatMessage({ id: 'validation.receiver.phone.required' }));
  }
  if (!deliveryAddress?.openTime) {
    errors.push(intl.formatMessage({ id: 'validation.receiver.openTime.required' }));
  }
  if (!deliveryAddress?.closeTime) {
    errors.push(intl.formatMessage({ id: 'validation.receiver.closeTime.required' }));
  }
  if (!deliveryAddress?.accessorialsArray || deliveryAddress?.accessorialsArray.length === 0) {
    errors.push(intl.formatMessage({ id: 'validation.receiver.accessorials.required' }));
  } else {
    if (deliveryAddress?.accessorials) {
      deliveryAddress.accessorials = cleanNoneData(deliveryAddress?.accessorials);
    }
  }

  return true;
}

export function isValidEmail(email: string) {
  const regex = /^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/;
  return regex.test(email);
}

export function cleanAccessorialsNode(formData: TruckPreOrder) {
  const shipperAddress = formData.shipperAddress;
  const deliveryAddress = formData.deliveryAddress;
  if (shipperAddress?.accessorials) {
    shipperAddress.accessorials = cleanNoneData(shipperAddress?.accessorials);
  }
  if (deliveryAddress?.accessorials) {
    deliveryAddress.accessorials = cleanNoneData(deliveryAddress?.accessorials);
  }
}

export function cleanNoneData(str: string) {
  const parts = str.split(',');
  const filteredParts = parts.filter(part => part.trim() !== "None");
  return filteredParts.join(',');
}

export function validateOrder(formData: TruckPreOrder, errors: string[], intl: IntlShape): boolean {

  if (formData.referenceNo && formData.referenceNo.length > 30) {
    errors.push(intl.formatMessage({ id: 'validation..truckOrder.referenceNo.maxLength' }));
  }
  if (formData.referenceNo && /[<>]/.test(formData.referenceNo)) {
    errors.push(intl.formatMessage({ id: 'validation.referenceNo.specialChars' }));
  }
  if (!formData.shipperAddressDT || !formData.deliveryAddressDT) {
    errors.push(intl.formatMessage({ id: 'validation.address.complete.required' }));
  }
  var v_shipper = validateAddress(formData.shipperAddressDT, intl.formatMessage({ id: 'label.shipper.address' }), intl);
  if (!v_shipper.isValid) {
    errors.push(v_shipper.message);
  }
  var v_delivery = validateAddress(formData.deliveryAddressDT, intl.formatMessage({ id: 'label.receiver.address' }), intl);
  if (!v_delivery.isValid) {
    errors.push(v_delivery.message);
  }
  for (let i = 0; i < formData.boxs.length; i++) {
    let box = formData.boxs[i];
    if (!box.boxUnit) {
      errors.push(intl.formatMessage({ id: 'validation.package.type.required.choice' }, { index: i + 1 }));
    }
    if (!box.boxQuantity) {
      errors.push(intl.formatMessage({ id: 'validation.package.quantity.required' }, { index: i + 1 }));
    }
    if (!box.boxWeight) {
      errors.push(intl.formatMessage({ id: 'validation.package.weight.required' }, { index: i + 1 }));
    }
    if (!box.boxLength) {
      errors.push(intl.formatMessage({ id: 'validation.package.length.required' }, { index: i + 1 }));
    }
    if (!box.boxWidth) {
      errors.push(intl.formatMessage({ id: 'validation.package.width.required' }, { index: i + 1 }));
    }
    if (!box.boxHeight) {
      errors.push(intl.formatMessage({ id: 'validation.package.height.required' }, { index: i + 1 }));
    }
    // if (!box.boxDescription) {
    //   errors.push(intl.formatMessage({ id: 'validation.package.description.required' }, { index: i + 1 }));
    // }
  }
  return errors.length === 0;
}

export function validateAddress(address: string | undefined | null, label: string, intl: IntlShape): AddressValidationResult {
  const pattern = /^\d+\s+[\w\s]+,\s*[\w\s]+,\s*[A-Z]{2}\s*\d{5}(-\d{4})?$/;

  if (!address || address.trim() === '') {
    return {
      isValid: false,
      message: intl.formatMessage({ id: 'validation.address.required' }, { label })
    };
  }

  if (!pattern.test(address.trim())) {
    return {
      isValid: false,
      message: intl.formatMessage({ id: 'validation.address.format' }, { label })
    };
  }

  return {
    isValid: true,
    message: ''
  };
}